package com.example.administrator.managesystem.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.bean.CameraListBean;
import com.example.administrator.managesystem.bean.LandListBean;
import com.example.administrator.managesystem.utils.GlobalConfig;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class GoodsGridLandAdapter extends BaseAdapter {
    private List<LandListBean.DataBean> myList=new ArrayList<>();
    private Context mContext;


    public GoodsGridLandAdapter(Context mContext) {
        this.mContext = mContext;
        // this.names=names;
    }
    public void addDatas(List<LandListBean.DataBean> list){
        this.myList=list;

    }


    @Override
    public int getCount() {
        return  myList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return  myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @SuppressLint("ResourceType")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


     ViewHolder viewHolder;
        if(convertView==null) {
            convertView=View.inflate(mContext, R.layout.land_grid_item, null);
            viewHolder=new ViewHolder();
            viewHolder.land_name=convertView.findViewById(R.id.land_name);
            viewHolder.land_icon = convertView.findViewById(R.id.land_icon);

            convertView.setTag(viewHolder);//store up viewHolder
        }else {

            viewHolder=(ViewHolder)convertView.getTag();
        }

        if(myList.get(position).getImage().isEmpty()){
            Picasso.with(mContext).load(R.drawable.undefine).into(viewHolder.land_icon);
        }else{
            Picasso.with(mContext).load(GlobalConfig.ONLINE_HTTP+""+myList.get(position).getImage()+"").into(viewHolder.land_icon);
        }

        viewHolder.land_name.setText(myList.get(position).getName()+"-"+myList.get(position).getGoods_name());
        viewHolder.land_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onItemClick(position,myList.get(position).getId(),myList.get(position).getName(),v);
            }
        });
        return convertView;
    }

    private class ViewHolder{
        TextView land_name;
        RoundedImageView land_icon;

    }


    protected OnItemClickListener mItemClickListener;


    public interface OnItemClickListener {
        void onItemClick(int position,String land_id,String land_name, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }


}
